package com.ssm.security.authorization;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.ssm.security.UserStatus2Redis;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by asd73 on 2017/8/4.
 */
public class MyLogoutSuccess implements LogoutSuccessHandler {
    static final String HEADER_REDIS_ID ="r_id";
    static final String HEADER_USER_NAME = "username";// 请求头用的参数username

    protected final Log logger = LogFactory.getLog(this.getClass());

    private UserStatus2Redis userStatus2Redis;

    public UserStatus2Redis getUserStatus2Redis() {
        return userStatus2Redis;
    }

    public void setUserStatus2Redis(UserStatus2Redis userStatus2Redis) {
        this.userStatus2Redis = userStatus2Redis;
    }

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String username = request.getHeader(HEADER_USER_NAME);
        String r_id=request.getHeader(HEADER_REDIS_ID);
        try {
            userStatus2Redis.deleteUserStatus(username,r_id);
            logger.info("user " +"'"+username+"'"+" delete user token 2 success ");
        } catch (IOException e) {
            logger.error("user " +"'"+username+"'"+" delete user token 2 redis failed cause: "+e.getMessage());
            throw new IOException(e.getMessage());
        }
    }
}
